#ifndef DATAPROC_H
#define DATAPROC_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "stdint.h"
#include "stdlib.h"
#include "string.h"
#include "mytype.h"

#define DATA_PROC_ERR_NONE 0x0
#define DATA_PROC_ERR_RES 0x21

#define uspr_with_speed(speed) ((60UL * 1000000UL + (speed >> 1)) / speed)					  
#define us20pr_with_speed(speed) ((60UL * 1000000UL + ((20 * speed) >> 1)) / (20 * speed))	  
#define us50pr_with_speed(speed) ((60UL * 1000000UL + ((50 * speed) >> 1)) / (50 * speed))	  
#define us100pr_with_speed(speed) ((60UL * 1000000UL + ((100 * speed) >> 1)) / (100 * speed)) 
#define rpm_with_us(us) ((60UL * 1000000UL + (us >> 1)) / us)								  
#define rpm_m10_with_us(us) ((600UL * 1000000UL + (us >> 1)) / us)							  
#define rpm_with_clk(clk, cnt) ((60UL * clk + (cnt >> 1)) / cnt)							  
#define rpm_m10_with_clk(clk, cnt) ((600UL * clk + (cnt >> 1)) / cnt)						  

#define logic_alloc() static uint8_t logic_handle_data
#define logic_and_init() logic_handle_data = 1
#define logic_or_init() logic_handle_data = 0
#define logic_and(x) logic_handle_data = logic_handle_data && (x)
#define logic_or(x) logic_handle_data = logic_handle_data || (x)
#define logic_not() logic_handle_data = !logic_handle_data
#define logic_get() logic_handle_data

#define lgc_new(x)			if((x)
#define lgc_next(x)			else if((x)
#define lgc_and(x) &&(x)
#define lgc_or(x) || (x)
#define lgc_exe()			)

	extern uint8_t const led_code[];
	extern uint8_t const ascii_code[];
	extern const uint8_t sinx_0r5pi_256_255[];
	extern uint16_t const sinx_0r5pi_2048_16384[];
	extern uint16_t const sinx2_0r5pi_2048_16384[];
	extern const char hex_ascii_h_tab[];
	extern const char hex_ascii_l_tab[];

	extern const uint8_t prime_b8[54];
	extern const uint16_t prime_b9[97];
	extern const uint16_t prime_b10[172];
	extern const uint16_t prime_b11[309];
	extern const uint16_t prime_b12[564];
	extern const uint16_t prime_b13[1028];
	extern const uint16_t prime_b14[1900];
	extern const uint16_t prime_b15[3512];
	extern const uint16_t prime_b16[6542];

	extern const char asc_pic_bat[];
	extern const char asc_pic_fo[];
	extern const char asc_pic_key[];
	extern const char asc_pic_girl[];
	extern const char asc_pic_shuai_cong[];
	extern const char asc_pic_shuai_cong2[];
	extern const char asc_pic_aple0807[];
	extern const char asc_pic_bat2[];
	extern const char asc_pic_bear[];
	extern const char asc_pic_fly[];
	extern const char asc_pic_light[];
	extern const char asc_pic_lights[];
	extern const char asc_pic_asctab[];
	extern const char asc_pic_ball[];

	int32_t dead_zone(int32_t val_real, int32_t val_now, int32_t dead_zone);

	void Hex4ToBCD4(uint16_t Data, uint8_t *Des);
	void Hex4ToBCD8(uint16_t Data, uint8_t *Des);
	void Hex2ToBCD2(uint8_t Data, uint8_t *Des);
	void Hex2ToBCD4(uint8_t Data, uint8_t *Des);

	uint8_t BCD2ToHex(uint8_t Data, uint8_t *Des);
	uint8_t BCD4ToHex(uint16_t Data, uint8_t *Des);
	uint8_t BCD8ToHex(uint32_t Data, uint8_t *Des);

	uint8_t bcd_to_hex_u8(uint8_t bcd);
	uint16_t bcd_to_hex_u16(uint16_t bcd);
	uint32_t bcd_to_hex_u32(uint32_t bcd);

	uint8_t hex_to_bcd_u8(uint8_t dat);
	uint16_t hex_to_bcd_u16(uint16_t dat);
	uint32_t hex_to_bcd_u32(uint32_t dat);

	void hex8_to_ascii(uint32_t Num, uint8_t *Des); 
	void hex4_to_ascii(uint16_t Num, uint8_t *Des); 
	void hex2_to_ascii(uint8_t Num, uint8_t *Des);	

	char hex_to_ascii_lo(uint8_t dat);
	char hex_to_ascii_ho(uint8_t dat);

	size_t hex2_to_ascii_multi(const uint8_t *hex, char *ascii, size_t len);

	size_t ascii_to_hex2_multi(const char *ascii, uint8_t *hex, size_t len_max);

	size_t str_to_hex2(const char *str, uint8_t *hex, size_t len_max);

	uint8_t ascii_to_hex(uint8_t ascii);

	uint8_t ascii_to_hex2(uint8_t *Src);  
	uint16_t ascii_to_hex4(uint8_t *Src); 
	uint32_t ascii_to_hex8(uint8_t *Src); 

	uint32_t code_set_lb(uint32_t src, uint8_t lb);
	uint32_t code_set_hb(uint32_t src, uint8_t hb);
	uint32_t code_set_ub(uint32_t src, uint8_t ub);
	uint32_t code_set_mb(uint32_t src, uint8_t mb);
	uint8_t code_get_lb(uint32_t src);
	uint8_t code_get_hb(uint32_t src);
	uint8_t code_get_ub(uint32_t src);
	uint8_t code_get_mb(uint32_t src);
	uint8_t code_add_u8(uint8_t u1, uint8_t u2);
	uint16_t code_add_u16(uint16_t u1, uint16_t u2);
	uint32_t code_add_u32(uint32_t u1, uint32_t u2);
	uint8_t code_add3_u8(uint8_t u1, uint8_t u2, uint8_t u3);
	uint16_t code_add3_u16(uint16_t u1, uint16_t u2, uint16_t u3);
	uint32_t code_add3_u32(uint32_t u1, uint32_t u2, uint32_t u3);
	uint8_t code_sub_u8(uint8_t u1, uint8_t u2);
	uint16_t code_sub_u16(uint16_t u1, uint16_t u2);
	uint32_t code_sub_u32(uint32_t u1, uint32_t u2);

	uint32_t get_2n_alain_val(uint32_t val);

	void i16_to_ascii(int16_t Num, uint8_t Attr, uint8_t *Des);
	void u16_to_ascii(uint16_t Num, uint8_t Attr, uint8_t *Des);

	uint8_t i32_to_string(void *des, int32_t ival, uint8_t ip_n, uint8_t attr);

	int32_t string_to_i32(void *src, uint8_t p_n);

	float str_to_float(const void *src);

	uint8_t u16_to_string(uint16_t Num, uint8_t Attr, uint8_t *Des);
	uint8_t i16_to_string(int16_t Num, uint8_t Attr, uint8_t *Des);

	void i16_to_led_code(int16_t Data, uint8_t *Des, uint8_t zero_clr, uint8_t PointSite, uint8_t nodisp);

	void i32_to_led_code(int32_t Data, uint8_t *Des, uint8_t zero_clr, uint8_t in_ip, uint8_t nodisp);

	uint8_t EvenU8(uint8_t Data);	  
	uint8_t OddU8(uint8_t Data);	  
	uint8_t NoneParity(uint8_t Data); 
#define even_u8 EvenU8
#define odd_u8 OddU8
#define parity_none NoneParity

	int32_t sum_i32(const void *dat, uint32_t len); 
	int32_t sum_i16(const void *dat, uint32_t len);
	int32_t sum_i8(const void *dat, uint32_t len);
	uint32_t sum_u32(const void *dat, uint32_t len);
	uint32_t sum_u16(const void *dat, uint32_t len);
	uint32_t sum_u8(const void *dat, uint32_t len);

	int32_t sub2_add8(float f_real, int32_t i_now);				   
	int32_t sub_add_x(float f_real, int32_t i_now, float val_sub); 
	int32_t int_sub2_add8(int32_t real, int32_t i_now);
	int32_t int_sub_add_x(int32_t real, int32_t i_now, int32_t val_sub);

	uint8_t lrc_u8(const void *dat, uint32_t len);	 
	uint16_t lrc_u16(const void *dat, uint32_t len); 
	uint32_t lrc_u32(const void *dat, uint32_t len); 

	uint8_t xor_u8(const void *dat, uint32_t len);	 
	uint16_t xor_u16(const void *dat, uint32_t len); 
	uint32_t xor_u32(const void *dat, uint32_t len); 

	void dat_add_u8(void *src, uint8_t data, uint32_t Len);	  
	void dat_add_u16(void *src, uint16_t data, uint32_t Len); 
	void dat_add_u32(void *src, uint32_t data, uint32_t Len); 
	void dat_sub_u8(void *src, uint8_t data, uint32_t Len);	  
	void dat_sub_u16(void *src, uint16_t data, uint32_t Len); 
	void dat_sub_u32(void *src, uint32_t data, uint32_t Len); 

	void record_u8(void *p_list, uint8_t dat, uint16_t *p_index, uint16_t Len);
	void record_u16(void *p_list, uint16_t dat, uint16_t *p_index, uint16_t Len);
	void record_u32(void *p_list, uint32_t dat, uint16_t *p_index, uint16_t Len);

	void record_in_first_u8(void *vp_list, uint8_t dat, uint16_t Len);
	void record_in_first_u16(void *vp_list, uint16_t dat, uint16_t Len);
	void record_in_first_u32(void *vp_list, uint32_t dat, uint16_t Len);

	int8_t bit1_get(uint32_t dat); 
	int8_t bit_1_num_get(uint32_t dat, uint8_t start_pos, uint8_t end_pos);
	int8_t bit_0_num_get(uint32_t dat, uint8_t start_pos, uint8_t end_pos);
	uint32_t list_bit_0_num_get(void *list, uint16_t len); 
	uint32_t list_bit_1_num_get(void *list, uint16_t len); 

	void map_add_u32(const uint32_t *src1, const uint32_t *src2, uint32_t *des, uint32_t len); 
	void map_sub_u32(const uint32_t *src1, const uint32_t *src2, uint32_t *des, uint32_t len); 
	void map_add_u16(const uint16_t *src1, const uint16_t *src2, uint16_t *des, uint32_t len); 
	void map_sub_u16(const uint16_t *src1, const uint16_t *src2, uint16_t *des, uint32_t len); 
	void map_add_u8(const uint8_t *src1, const uint8_t *src2, uint8_t *des, uint32_t len);	   
	void map_sub_u8(const uint8_t *src1, const uint8_t *src2, uint8_t *des, uint32_t len);	   

	void map_xor_u8(const uint8_t *src1, const uint8_t *src2, uint8_t *des, uint32_t len);	   
	void map_xor_u16(const uint16_t *src1, const uint16_t *src2, uint16_t *des, uint32_t len); 
	void map_xor_u32(const uint32_t *src1, const uint32_t *src2, uint32_t *des, uint32_t len); 

	typedef struct
	{
		uint8_t IncCoef; 
		uint8_t bDir;
		uint16_t min; 
		uint16_t max; 
		uint16_t out;
		uint16_t min_keep_time_sv; 
		uint16_t max_keep_time_sv; 
		uint16_t min_keep_time_cnt;
		uint16_t max_keep_time_cnt;
		uint32_t Cal;
	} led_breath_type;
	uint32_t led_breath_handle(led_breath_type *Brj);

	typedef struct
	{
		int32_t time_set;	 
		int32_t time_cnt;	 
		int32_t time_remain; 
		uint16_t step_now;	 
		uint16_t step_req;	 
		uint8_t st_new_step; 
		uint8_t st_time_end; 
	} step_manage_type;

	void step_manage_handle(step_manage_type *obj, uint8_t time_pulse);
	void step_manage_reset(step_manage_type *obj);
	void step_manage_req(step_manage_type *obj, uint16_t step_req);

	i32_limit_type list_dir_i32(const void *dat, uu16 len, uu16 index_now, uu16 index_offset, uu16 amount);
	i16_limit_type list_dir_i16(const void *dat, uu16 len, uu16 index_now, uu16 index_offset, uu16 amount);

	int32_t dat32_rang_mod(int32_t min, int32_t max, int32_t dat);
	int16_t dat16_rang_mod(int16_t min, int16_t max, int16_t dat);

	uu8 rom_write(uu32 addr, uu8 *dat, uu32 len, uu32 blk_size, uu8 (*wr_hal)(uu32, uu8 *, uu32));

#ifdef __cplusplus
}
#endif

#endif 
